//
//  ThirdPartWeibo.m
//  Screen
//
//  Created by Season02 on 6/22/16.
//  Copyright © 2016 Season02. All rights reserved.
//

#import "ThirdPartWeibo.h"
#import "SeWeiboEntity.h"
#import "ThirdBaseEntity.h"
//#import "MGCLoginRegisterViewController.h"
//#import "MGCTabBarController.h"

//#import <MJExtension.h>
#import "sonic-Swift.h"

@implementation ThirdPartWeibo

+(void)initWB
{
    [WeiboSDK enableDebugMode:NO];
    [WeiboSDK registerApp:SeThirdWeibo_AppKey];
}

+(BOOL)checkWB
{
    return [WeiboSDK isWeiboAppInstalled];
}

-(BOOL)handWBUrl:(NSURL* )url
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

+(void)authorizeWB
{
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = SeThirdWeibo_RedirectURI;
    request.scope = @"all";
    request.userInfo = @
    {
        @"SSO_From" : @"SendMessageToWeiboViewController",
        @"Other_Info_1" : [NSNumber numberWithInt:123],
        @"Other_Info_2" : @[@"obj1" , @"obj2"],
        @"Other_Info_3" : @{@"key1" : @"obj1" , @"key2" : @"obj2"}
    };
    [WeiboSDK sendRequest:request];
}

/**
 收到一个来自微博客户端程序的请求
 
 收到微博的请求后，第三方应用应该按照请求类型进行处理，处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
 @param request 具体的请求对象
 */
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

/**
 收到一个来自微博客户端程序的响应
 
 收到微博的响应后，第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
 @param response 具体的响应对象
 */
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
//    NSLog(@"微博的response = %@", response.userInfo);
    
//    APP_DELEGATE.loginVC = nil;
    if ([response isKindOfClass:WBAuthorizeResponse.class])
    {
        if ((int)response.statusCode == 0)
        {
            NSString *openId = [(WBAuthorizeResponse *)response userID];
            NSString *toke = [(WBAuthorizeResponse *)response accessToken];
            
            [WBHttpRequest requestWithAccessToken:toke url:@"https://api.weibo.com/2/users/show.json" httpMethod:@"GET" params:[NSDictionary  dictionaryWithObject:openId forKey:@"uid"] delegate:(id)self withTag:@"hello_xixi"];
            
        }
    }
    
}

- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
{
    NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//转换数据格式
//    NSLog(@"%@ i am result",content); //这里会返回 一些Base Info
    
//    SeWeiboEntity *wei = [SeWeiboEntity mj_objectArrayWithKeyValuesArray:content];
    
    SeWeiboEntity *wei = [SeWeiboEntity initWithDict:content];
    
//    if([MGCLoginRegisterViewController genericLoginProcess:wei])
//    {
//        //UIApplication.sharedApplication.delegate.window.rootViewController = [[MGCTabBarController alloc] init];
//    }
}

//- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
//{
//    NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];//转换数据格式
//    //    NSLog(@"%@ i am result",content); //这里会返回 一些Base Info
//    
//    //    SeWeiboEntity *wei = [SeWeiboEntity mj_objectArrayWithKeyValuesArray:content];
//    
//    SeWeiboEntity *wei = [SeWeiboEntity initWithDict:content];
//    
////    if([MGCLoginRegisterViewController genericLoginProcess:wei])
////    {
////        //UIApplication.sharedApplication.delegate.window.rootViewController = [[MGCTabBarController alloc] init];
////    }
//}

@end
